package com.atguigu.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * create by Shipeixin on  2021-05-26  13:51
 */
public class CountInterceptor implements ProducerInterceptor<String,String> {

    int successCount = 0;
    int errorCount = 0;

    @Override
    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> record) {
        return record;
    }

    @Override
    public void onAcknowledgement(RecordMetadata metadata, Exception exception) {
        if (metadata != null){
            successCount++;
        }else{
            errorCount++;
        }
    }

    @Override
    public void close() {
        System.out.println("success:" + successCount);
        System.out.println("error:" + errorCount);
    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
